package com.lyf.scm.admin.domain.remote.allocationservice.facade;

import com.lyf.scm.admin.api.dto.BaseSkuInfoDTO;
import com.lyf.scm.admin.api.dto.SkuPriceDTO;
import com.lyf.scm.admin.api.dto.UnitRateQueryDTO;
import com.lyf.scm.admin.domain.remote.allocationservice.RedisCacheRemote;
import com.lyf.scm.admin.domain.remote.allocationservice.dto.SkuUnitDTO;
import com.lyf.scm.admin.domain.remote.allocationservice.dto.BaseAreaInfoDTO;
import com.lyf.scm.admin.domain.remote.basedata.dto.ChannelDTO;
import com.lyf.scm.admin.domain.remote.basedata.dto.StoreDTO;
import com.lyf.scm.common.enums.ResponseMsg;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.core.exception.RomeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 缓存
 *
 * @author wangchlei
 * @date 2020/11/07
 */
@Service
@Slf4j
public class RedisCacheFacade {
    @Resource
    private RedisCacheRemote redisCacheRemote;

    public Map<String, ChannelDTO> getAllChannelInfo() {
        Response<Map<String, ChannelDTO>> response = redisCacheRemote.getAllChannelInfo();
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    public Map<String, StoreDTO> getStoreInfoByCodes(List<String> codes) {
        Response<Map<String, StoreDTO>> response = redisCacheRemote.getStoreInfoByCodes(codes);
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }


    public Map<String, BigDecimal> getSkuAveragePrice(List<SkuPriceDTO> skuPriceDTOs) {
        Response<Map<String, BigDecimal>> response = redisCacheRemote.getSkuAveragePrice(skuPriceDTOs);
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    public Map<String, BaseSkuInfoDTO> getCategoryInfoBySkuCodes(List<String> skuCodes) {
        Response<Map<String, BaseSkuInfoDTO>> response = redisCacheRemote.getCategoryInfoBySkuCodes(skuCodes);
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    public Map<String, SkuUnitDTO> getUnitConversionRatio(List<UnitRateQueryDTO> unitRateQueryDTOs) {
        Response<Map<String, SkuUnitDTO>> response = redisCacheRemote.getUnitConversionRatio(unitRateQueryDTOs);
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    /**
     * 获取区域信息（门店或省市区）
     *
     * @param codes
     * @param type  1：省市区  2：门店
     * @return {@link Map<String,String>}
     **/
    public Map<String, BaseAreaInfoDTO> getAreaInfo(List<String> codes, Integer type) {
        Response<Map<String, BaseAreaInfoDTO>> response = redisCacheRemote.getAreaInfo(codes, type);
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    public Map<String, StoreDTO> getDeliverWarehouse() {
        Response<Map<String, StoreDTO>> response = redisCacheRemote.getDeliverWarehouse();
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }
}
